﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GShare1._0.Client
{
    public partial class GUI_Client : Form
    {
        MainThreadInvoker _invoker;
        MessageBoxInvoker mboxInvoker;
        public AutoResetEvent autoReset = new AutoResetEvent(false);

        public GUI_Client()
        {
            InitializeComponent();
            _invoker = new MainThreadInvoker(OfflineEvent);
            mboxInvoker = new MessageBoxInvoker(MessageDel);
            this.Shown += GUI_Client_Shown;
            this.FormClosing += GUI_Client_FormClosing;
        }

        void GUI_Client_FormClosing(object sender, FormClosingEventArgs e)
        {
            autoReset = new AutoResetEvent(false);
        }

        void GUI_Client_Shown(object sender, EventArgs e)
        {
            this.autoReset.Set();
        }

        private void MessageDel(string msg)
        {
            MethodInvoker invoker = new MethodInvoker(delegate
            {
                textBox3.Text += msg + Environment.NewLine;
            });
            if (!this.Visible)
            {
                this.autoReset.WaitOne();
            }
            if (this.Visible)
            {
                this.BeginInvoke(invoker);
            }
            
                
                
        }

        public ClientConnection client;

        public void tryConnect(ClientConnection client)
        {
            try
            {
                //Set Delegates for UI
                client.MessageInvoker = mboxInvoker;
                client.Invoker = _invoker;

                client.Connect();
                autoReset.WaitOne(5000);
                //needs more specialized way of connecting...
                if (client.Connected == false)
                {
                    client.Socket.Dispose();
                    client.Socket.Close();
                    MessageBox.Show("Connection Failed");
                    this.Close();
                }
                else
                {
                    this.client = client;
                    this.ShowDialog();
                    
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            if (client != null)
            {
                if (client.Download.Alive != true)
                {
                    client.UploadFile();
                }
                
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (client != null && !string.IsNullOrEmpty(textBox4.Text))
            {
                client.SendMessage("TEXT",textBox4.Text);
                this.textBox4.Clear();
            }
        }

        private void OfflineEvent()
        {
            MethodInvoker invoker = new MethodInvoker(delegate
                {
                    this.Close();
                });
            //Form has no handle and cannot invoke on main thread yet 
            if (this.Visible)
            {
                this.BeginInvoke(invoker);
            }
            
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Boolean flag used to determine when a character other than a number is entered. 
            if ((Keys)e.KeyChar == Keys.Enter)
            {
                button2_Click_1(this, new EventArgs());
                textBox4.Refresh();
            }
            

        }
    }
}
